<?php 

namespace Common\Model;
use Common\Model\CommonModel;

/**
 * 菜单栏
 */
class AdminNavModel extends CommonModel
{
	
	/**
	 * 获取用户进入时显示的菜单
	 */
	public function getTreeData($type="tree", $order="")
	{
		// 判断是否需要排序
		if(empty($order)){
			$data=$this->select();
		}else{
			$data=$this->order('order_number is null,'.$order)->select();
		}
		// 获取树形或者结构数据
		if($type=='tree'){
			$data=\Org\Nx\Data::tree($data,'name','id','pid');
		}elseif($type="level"){
			$data=\Org\Nx\Data::channelLevel($data,0,'&nbsp;','id');
			// var_dump($data);
			// die;
			// 显示有权限的菜单
			if (intval($_SESSION['userinfo']['id']) !== 1) {
				// echo 1;
				$auth=new \Think\Auth();
				foreach ($data as $k => $v) {
					// 检测权限
					
						if ($auth->check($v['mca'],$_SESSION['userinfo']['id'])) {
							foreach ($v['_data'] as $m => $n) {
								if(!$auth->check($n['mca'],$_SESSION['userinfo']['id'])){
									unset($data[$k]['_data'][$m]);
								}
							}
						}else{
							// 删除无权限的菜单
							unset($data[$k]);
						}
				}
			}
		}
		// var_dump($_SESSION['userinfo']["id"]);
		// var_dump(session("userinfo"));
		// print_r($data);
		// die;
		return $data;
	}

}